package xgenerator.ui.swing.adapter;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import xgenerator.ui.swing.LogonFrame;
import xgenerator.ui.swing.XGeneratorFrame;

public class LogonAdapter extends AbstractAction {
	
	public static final String USERNAME_COMMAND = "username";
	public static final String PASSWORD_COMMAND = "password";
	public static final String LOGON_COMMAND = "logon";
	public static final String RESET_COMMAND = "reset";

	/**
	 * UI界面
	 */
	private LogonFrame logonFrame;
	
	public LogonAdapter(LogonFrame logonFrame) {
		this.logonFrame = logonFrame;
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		final String actionCommand = e.getActionCommand();
		if(USERNAME_COMMAND.equals(actionCommand)) {
			usernameActionPerformed(e);
		} else if(PASSWORD_COMMAND.equals(actionCommand)) {
			passwordActionPerformed(e);
		} else if(LOGON_COMMAND.equals(actionCommand)) {
			logonActionPerformed(e);
		} else if(RESET_COMMAND.equals(actionCommand)) {
			resetActionPerformed(e);
		}
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void usernameActionPerformed(ActionEvent e) {
		
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void passwordActionPerformed(ActionEvent e) {
		
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void logonActionPerformed(ActionEvent e) {
		try {
			String userName = this.logonFrame.getUserNameField().getText();
			String password = new String(this.logonFrame.getPasswordField().getPassword());
			if (null == userName || "".equals(userName.trim())) {
				this.logonFrame.getUserNameField().setFocusable(true);
				throw new IllegalArgumentException("用户名为空！");
			}
			if (null == password || "".equals(password.trim())) {
				this.logonFrame.getPasswordField().setFocusable(true);
				throw new IllegalArgumentException("密码为空！");
			}

			if (securityCheck(userName, password)) {
				new XGeneratorFrame();
				this.logonFrame.dispose();
			} else {
				JOptionPane.showMessageDialog(this.logonFrame, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(this.logonFrame, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public boolean securityCheck(String userName, String password) {
		if ("admin".equals(userName) && "admin".equals(password)) {
			return true;
		}
		return false;
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void resetActionPerformed(ActionEvent e) {
		this.logonFrame.getUserNameField().setText("");
		this.logonFrame.getPasswordField().setText("");
	}

}
